package com.ld.hpfs.web.controller;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.HistoryService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class DealHistoryController {
	private static final Logger logger = LoggerFactory.getLogger(DealHistoryController.class);

	@Autowired
	private HistoryService historyService;		
	
	@RequestMapping(value = "/getDealHistory")
    public void getDealHistory(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		String cpnId = session.getAttribute(WebConstants.CURRENT_COMPANY).toString(); 
		try {
			HashMap<String,Object> param = new HashMap<String,Object>();
			HashMap<String,Object> result = new HashMap<String,Object>();
			String document_no = (String) request.getParameter("document_no") == null ? null : (String)request.getParameter("document_no");
			// 获得语言环境
			String language = (String) session.getAttribute(WebConstants.CURRENT_LANG);
			
			// false时，只显示此票据履历动作；true时，显示此票据及拆分、合并前的所有履历动作
			param.put("cpnId", cpnId);
			param.put("document_no", document_no);
			param.put("flag", true);
			param.put("language", language);
			ArrayList<HashMap<String,Object>> histories =historyService.getHistoryByHeadid(param);	
			
			result.put("success", true);
			if (histories != null && histories.size() > 0) {
                result.put("data", histories);                  
            } else {
                result.put("data", "");
            }
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
    }
	
	@RequestMapping(value = "/getDealHeadHistory")
    public void getHeadHistory(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		try {
			HashMap<String,Object> result = new HashMap<String,Object>();
			String histId = (String) request.getParameter("histId") == null ? null : (String)request.getParameter("histId");
			boolean flag = Integer.parseInt(request.getParameter("flag")) == 1;
			// 获得语言环境
			String language = (String) session.getAttribute(WebConstants.CURRENT_LANG);
			ArrayList<HashMap<String,Object>> histories =historyService.getInvoiceHeadHistoryByHistid(Integer.parseInt(histId), flag, language);	
			result.put("success", true);
			if (histories != null && histories.size() > 0) {
                result.put("data", histories);      
            } else {
                result.put("data", "");
            }
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
    }
	
	@RequestMapping(value = "/getDealDetailHistory")
    public void getDetailHistory(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		try {
			HashMap<String,Object> result = new HashMap<String,Object>();
			String histId = (String) request.getParameter("histId") == null ? null : (String)request.getParameter("histId");
			boolean flag = Integer.parseInt(request.getParameter("flag")) == 1;
			ArrayList<HashMap<String,Object>> histories =historyService.getInvoiceDetailHistoryByHistid(Integer.parseInt(histId), flag);	
			result.put("success", true);
			if (histories != null && histories.size() > 0) {
                result.put("data", histories);      
            } else {
                result.put("data", "");
            }
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
    }
}